package com.GlobalPaint.app.ui.Mine;

import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.QianDaoBean;
import com.GlobalPaint.app.bean.QianDaoDayBean;
import com.GlobalPaint.app.bean.QiandaoBodayBean;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.ui.MainActivity;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.github.airsaid.calendarview.widget.CalendarView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class QiandaoActivity extends AppCompatActivity implements View.OnClickListener{
    public static final int GET_QIANDAO = 0x0003;//签到
    public static final int GET_PANDUAN = 0x0004;//判断是否签到
    public static final int GET_DAY = 0x0005;//签到天数
    private QianDaoBean bean;
    private TextView tv_lianxu;
    private Handler mhandler = new Handler() {
        public void handleMessage(Message message) {
            switch (message.what) {
                case GET_QIANDAO:
                    String url2= Constants.JASON_SERVICE_URL+"appSign/insertAppSign?userId="+ DataManager.userEntity.getUserId();
                    DataManager.getInst().getHttpRequestJsonClass(url2, DataManager.userEntity.getCookie(), QianDaoBean.class, new DataManager.Callback() {
                        @Override
                        public void onSuccess(Object o) {
                            bean=(QianDaoBean)o;
                            if (bean.getStatus() == 1){
                                mhandler.sendEmptyMessage(GET_PANDUAN);
                                Toast.makeText(QiandaoActivity.this,"签到成功",Toast.LENGTH_LONG).show();
                            }
                            else {
                                Toast.makeText(QiandaoActivity.this,bean.getMsg(),Toast.LENGTH_LONG).show();
                            }
                        }

                        @Override
                        public void onFailure(ReturnStatusResultEntity error) {
                        }
                    });
                    break;
                case GET_PANDUAN:
                    String url= Constants.JASON_SERVICE_URL+"appSign/getCurrentSign?userId="+ DataManager.userEntity.getUserId();
                    DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), QianDaoBean.class, new DataManager.Callback() {
                        @Override
                        public void onSuccess(Object o) {
                            bean=(QianDaoBean)o;
                            if (bean.getStatus() == 1){
                                if(bean.getData() != null ){
                                    qiandao.setVisibility(View.VISIBLE);
                                    weiqiandao.setVisibility(View.GONE);
                                   tv_lianxu.setText("连续签到"+bean.getData().getNum()+"天");
                                    mTxtDate.setText(mCalendarView.getYear() + "年" + (mCalendarView.getMonth() + 1) + "月");
                                    String now = getNow();
                                    start=getFirstDayOfMonth(mCalendarView.getYear(),mCalendarView.getMonth() + 1)+"+00:00:00";
                                    end=now+"+23:59:59";
                                    mhandler.sendEmptyMessage(GET_DAY);
                                }else {
                                    qiandao.setVisibility(View.GONE);
                                    weiqiandao.setVisibility(View.VISIBLE);
                                    tv_lianxu.setText("未签到");
                                }
                            }
                            else {
                                Toast.makeText(QiandaoActivity.this,bean.getMsg(),Toast.LENGTH_LONG).show();
                            }
                        }

                        @Override
                        public void onFailure(ReturnStatusResultEntity error) {
                        }
                    });
                    break;
                case GET_DAY:
                    String urlday= Constants.JASON_SERVICE_URL+"appSign/querySignList?userId="+DataManager.userEntity.getUserId()+"&beginTime="+
                            start+"&endTime="+end;
                    DataManager.getInst().getHttpRequestJsonClass(urlday, DataManager.userEntity.getCookie(), QianDaoDayBean.class, new DataManager.Callback() {
                        @Override
                        public void onSuccess(Object o) {
                            list = (QianDaoDayBean) o;
                            if (list.getStatus() == 1) {
                                listben = new ArrayList<>();
                                if (list.getData().size() > 0) {
                                    for (int i = 0; i < list.getData().size(); i++) {
                                        String timesub=list.getData().get(i).getCreateTime().substring(0, 10);//获取前10为
                                        listben.add(timesub.replaceAll("[^0-9a-zA-Z\u4e00-\u9fa5.，,。？“”]+",""));// 去掉特殊符号
                                        mCalendarView.setSelectDate(listben);
                                    }
                                }

                            }
                        }
                        @Override
                        public void onFailure(ReturnStatusResultEntity error) {
                        }
                    });
                    break;
            }
        }
    };
    private ImageView qiandao;
    private ImageView weiqiandao;
    private QianDaoDayBean list;
    private List<String> listben;
    private static final String TAG = MainActivity.class.getSimpleName();

    private CalendarView mCalendarView;
    private TextView mTxtDate;
    private String start;
    private String end;
    SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
    private List<String> stringList;
    private QiandaoBodayBean bodayBean;
    private ImageView iv_goback;
    private TextView tv_common_center;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qiandao);
        initView();
        mhandler.sendEmptyMessage(GET_PANDUAN);

    }

    private void initView() {
        iv_goback = (ImageView) findViewById(R.id.iv_goback);
        iv_goback.setOnClickListener(this);
        tv_common_center = (TextView) findViewById(R.id.tv_common_center);
        tv_common_center.setText("签到");
        qiandao = (ImageView)findViewById(R.id.qiandao);
        qiandao.setOnClickListener(this);
        weiqiandao = (ImageView) findViewById(R.id.weiqiandao);
        weiqiandao.setOnClickListener(this);
        tv_lianxu = (TextView) findViewById(R.id.tv_lianxu);
        //日历参数
        mTxtDate = (TextView) findViewById(R.id.txt_date);
        mCalendarView = (CalendarView) findViewById(R.id.calendarView);

        // 设置已选的日期

        // 指定显示的日期, 如当前月的下个月
        Calendar calendar = mCalendarView.getCalendar();
        calendar.add(Calendar.MONTH, 0);
        mCalendarView.setCalendar(calendar);

        // 设置字体
        mCalendarView.setTypeface(Typeface.SERIF);
        // 设置是否能够改变日期状态
        mCalendarView.setChangeDateStatus(false);

        // 设置日期点击监听
        mCalendarView.setOnDataClickListener(new CalendarView.OnDataClickListener() {
            @Override
            public void onDataClick(@NonNull CalendarView view, int year, int month, int day) {
                Log.e(TAG, "year: " + year);
                Log.e(TAG, "month,: " + (month + 1));
                Log.e(TAG, "day: " + day);
            }
        });
        // 设置是否能够点击
        mCalendarView.setClickable(false);

        setCurDate();
    }
    /**
     * 获取当前日期
     *
     * @return
     */
    private String getNow() {
        Date dt = new Date();

        return matter1.format(dt);
    }
    //第一次进入 赋值 start end
    private void setCurDate(){
        mTxtDate.setText(mCalendarView.getYear() + "年" + (mCalendarView.getMonth() + 1) + "月");
        String now = getNow();
        start=getFirstDayOfMonth(mCalendarView.getYear(),mCalendarView.getMonth() + 1)+"+00:00:00";
        end=now+"+23:59:59";
        mhandler.sendEmptyMessage(GET_DAY);
        Log.d("dddd","start="+start );
        Log.d("dddd","end="+end );
    }
    public void next(View v){
        mCalendarView.nextMonth();
        mTxtDate.setText(mCalendarView.getYear() + "年" + (mCalendarView.getMonth() + 1) + "月");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        start=getFirstDayOfMonth(mCalendarView.getYear(),mCalendarView.getMonth() + 1)+"+00:00:00";
        end=getLastDayOfMonth(mCalendarView.getYear(),mCalendarView.getMonth() + 1)+"+23:59:59";
        mhandler.sendEmptyMessage(GET_DAY);
        Log.d("dddd","start="+start );
        Log.d("dddd","end="+end );
    }

    public void last(View v){
        mCalendarView.lastMonth();
        mTxtDate.setText(mCalendarView.getYear() + "年" + (mCalendarView.getMonth() + 1) + "月");
        start=getFirstDayOfMonth(mCalendarView.getYear(),mCalendarView.getMonth() + 1)+"+00:00:00";
        end=getLastDayOfMonth(mCalendarView.getYear(),mCalendarView.getMonth() + 1)+"+23:59:59";
        mhandler.sendEmptyMessage(GET_DAY);
        Log.d("dddd","start="+start );
        Log.d("dddd","end="+end );
    }
    /**
     * 获得该月第一天
     * @param year
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(int year,int month){
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String firstDayOfMonth = sdf.format(cal.getTime());
        return firstDayOfMonth;
    }
    /**
     * 获得该月最后一天
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int year,int month){
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth;
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.weiqiandao:
            case R.id.qiandao:
                mhandler.sendEmptyMessage(GET_QIANDAO);
                break;
            case R.id.iv_goback:
                finish();
                break;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
